package java8stream;

import com.alibaba.fastjson.JSON;

import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * Created by ZhuQianli on 2018/1/17.
 */
public class Generate {
    public static void main(String[] args) throws InterruptedException {
        // 生成10个随机整数
        Stream.generate(new Random()::nextInt).limit(10).forEach(System.out::println);

        System.out.println("######################");
        Consumer<Integer> consumer = System.out::println;
        consumer = consumer.andThen(System.out::println);
        Stream.generate(new Generate()::nextInt).limit(3).forEach(consumer);


        PersonSupplier personSupplier = new PersonSupplier();
        Stream.generate(personSupplier).limit(10).forEach(person -> System.out.println(JSON.toJSONString(person)));
        Stream.generate(personSupplier).limit(10).forEach(person -> System.out.println(JSON.toJSONString(person)));


    }

    static class PersonSupplier implements Supplier<Person> {
        Integer sequenceNumber = 0;

        @Override
        public Person get() {
            return new Person("name" + ++sequenceNumber, sequenceNumber * 2);
        }
    }

    static class Person {
        String name;
        Integer age;

        public String getName() {
            return name;
        }

        public Person setName(String name) {
            this.name = name;
            return this;
        }

        public Integer getAge() {
            return age;
        }

        public Person setAge(Integer age) {
            this.age = age;
            return this;
        }

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    }

    Random r =  new Random();
    public int nextInt() {
        return r.nextInt(111);
    }

}
